package com.wanmait.movie.exception;

import com.auth0.jwt.exceptions.JWTVerificationException;
import com.wanmait.movie.util.AjaxResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.annotation.Resource;
import javax.validation.ConstraintViolationException;
import java.util.Objects;

@RestControllerAdvice
public class ProjectException extends Exception{
    @ExceptionHandler(ConstraintViolationException.class)
    public AjaxResult handleConstraintViolationException(ConstraintViolationException e) {
        return AjaxResult.error(e.getMessage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public AjaxResult handleException(MethodArgumentNotValidException e) {
        return AjaxResult.error(Objects.requireNonNull(e.getFieldError()).getDefaultMessage());
    }
    @ExceptionHandler(JWTVerificationException.class)
    public AjaxResult handleJWTVerificationException(JWTVerificationException e) {
        return AjaxResult.error(e.getMessage());
    }
}
